package com.mxpio.erp.technology.controller;


import com.mxpio.erp.common.technology.entity.WorkTeamMember;
import com.mxpio.erp.common.technology.service.WorkTeamMemberService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import com.mxpioframework.security.entity.User;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @author wupaa
 */
@Tag(name = "WorkTeamMemberController", description = "班组信息成员信息接口")
@RestController
@RequestMapping("/erp/tech/workteam/member/")
public class WorkTeamMemberController {

    @Autowired
    private WorkTeamMemberService service;

    @GetMapping("list")
    @Operation(summary = "班组信息成员列表", description = "获取班组信息成员列表（无分页）", method = "GET")
    public Result<List<WorkTeamMember>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<WorkTeamMember> lines = service.list(WorkTeamMember.class, criteria);
        return Result.OK(lines);
    }

    @GetMapping("page")
    @Operation(summary = "班组信息成员列表", description = "获取班组信息成员列表（分页）", method = "GET")
    public Result<Page<WorkTeamMember>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<WorkTeamMember> lines = service.listPage(WorkTeamMember.class, page, criteria);
        return Result.OK(lines);
    }

    @GetMapping("list/{workTeamCode}")
    @Operation(summary = "根据workTeamCode获取班组信息成员", description = "根据workTeamCode获取班组信息成员", method = "GET")
    public Result<List<WorkTeamMember>> listByBizNo(@Parameter(description="班组编号") @PathVariable(name = "workTeamCode", required = true) String workTeamCode) {
        Criteria c = Criteria.create().addCriterion("workTeamCode", Operator.EQ, workTeamCode);
        List<WorkTeamMember> lines = service.list(WorkTeamMember.class,c);
        return Result.OK(lines);
    }

    @GetMapping("unrelated_member/page/{workTeamCode}")
    @Operation(summary = "班组信息成员未关联人员", description = "获取班组信息成员未关联人员列表(分页)", method = "GET")
    public Result<Page<User>> getUnrelatedMemberPage(@Parameter(description="班组编号") @PathVariable(name = "workTeamCode", required = true) String workTeamCode,
                                                     Criteria criteria, Integer pageSize, Integer pageNo) {
        Pageable pageAble = PageRequest.of(pageNo-1, pageSize);
        Page<User> page = service.getUnrelatedMemberPage(workTeamCode,pageAble, criteria);
        return Result.OK(page);
    }

}
